package com.woniuxy.authserver.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Data
public class User implements UserDetails, Serializable {
    private Integer id;
    private String account;
    @JsonIgnore
    private String password;
    private String avatar;
    private String cellphone;
    private String email;
    private String registdate;
    private String lastlogin;
    private String state;

    private Role role;

    // 获取当前用户的所有角色和权限信息
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        // 封装角色信息
        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(this.role.getName());
        authorities.add(simpleGrantedAuthority);
        // 封装权限信息
        for (Perms perms : role.getPermsList()) {
            simpleGrantedAuthority = new SimpleGrantedAuthority(perms.getName());
            //
            authorities.add(simpleGrantedAuthority);
        }
        return authorities;
    }

    // 获取账号
    @JsonIgnore
    @Override
    public String getUsername() {
        return this.account;
    }

    // 当前账号是否没过期
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 当前账号是否没被锁定、封号
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 凭证是否没过期
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 用户账号是否可用
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
